package com.audaque.springboot.foshanupload.core.util;

import com.google.common.base.CaseFormat;

import java.util.*;

/**
 * @author zgb
 * @desc ...
 * @date 2023-12-05 19:08:04
 */
public class MapUtils {


    /**
     * 把key从小写camel转成小写underscore
     *
     * @param map
     * @return
     */
    public static Map<String, Object> convertKeyToLowerUnderscore(Map<String, Object> map) {
        return map.entrySet().stream().collect(
                HashMap::new, (m, v) ->
                        m.put(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, v.getKey()), v.getValue()), HashMap::putAll
        );
    }
    /**
     * 添加map的list方法
     *
     * @param listMap
     * @param key
     * @param newItem
     * @param <K>
     * @param <ITEM>
     */
    public static <K, ITEM> void addMapListItem(Map<K, List<ITEM>> listMap, K key, ITEM newItem) {
        List<ITEM> list = listMap.getOrDefault(key, new ArrayList<>());
        list.add(newItem);
        listMap.put(key, list);
    }

    public static <K, ITEM> void addMapSetItem(Map<K, Set<ITEM>> listMap, K key, ITEM newItem) {
        Set<ITEM> list = listMap.getOrDefault(key, new HashSet<>());
        list.add(newItem);
        listMap.put(key, list);
    }

    public static <K, ITEM> void addMapSetItems(Map<K, Set<ITEM>> listMap, K key, Collection<ITEM> newItems) {
        Set<ITEM> list = listMap.getOrDefault(key, new HashSet<>());
        list.addAll(newItems);
        listMap.put(key, list);
    }

}
